import 'package:flutter/material.dart';
import 'package:flutter_demo/chapter7-FunctionWidget/dialog/DialogCheckBox.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Function Widget Demo"),
      ),
      body: ShowDeleteConfirmDialog3Widget(),
    ),
  ));
}

class ShowDeleteConfirmDialog3Widget extends StatelessWidget {
  bool _withTree = false;

  ShowDeleteConfirmDialog3Widget({Key? key}) : super(key: key); // 复选框选中状态

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ElevatedButton(
          child: const Text("对话框3, 可以选中"),
          onPressed: () async {
            bool? delete = await showDeleteConfirmDialog3(context);
            if (delete == null) {
              print("取消删除");
            } else {
              print("同时删除子目录： $delete");
            }
          },
        ),
      ],
    );
  }

  Future<bool?> showDeleteConfirmDialog3(context) {
    _withTree = false; // 默认复选框不选中
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text("提示"),
          content: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              const Text("您确定要删除当前文件吗?"),
              Row(
                children: <Widget>[
                  const Text("同时删除子目录？"),
                  DialogCheckBox(
                    value: _withTree,
                    onChanged: (value) {
                      _withTree = !_withTree;
                    },
                  ),
                ],
              ),
            ],
          ),
          actions: <Widget>[
            TextButton(
              child: const Text("取消"),
              onPressed: () => Navigator.of(context).pop(),
            ),
            TextButton(
              child: const Text("删除"),
              onPressed: () {
                //执行删除操作
                Navigator.of(context).pop(_withTree);
              },
            ),
          ],
        );
      },
    );
  }
}
